package com.yunxi.service.dubboService.impl.dealer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageInfo;
import com.yunxi.dealer.pojo.AreaManagerVO;
import com.yunxi.dealer.service.AreaManagerDubboService;
import com.yunxi.model.dealer.AreaManager;
import com.yunxi.service.localService.dealer.AreaManagerService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author
 * @since 2019-07-23
 */
public class AreaManagerDubboServiceImpl implements AreaManagerDubboService {

   @Autowired
   @Qualifier(value = "areaManagerServiceImpl")
   AreaManagerService areaManagerService;


   @Override
   public AreaManagerVO getById(String id) throws InvocationTargetException, IllegalAccessException {
      AreaManager areaManager = areaManagerService.getById(id);
      AreaManagerVO areaManagerVO = new AreaManagerVO();
      BeanUtils.copyProperties(areaManager, areaManagerVO);
      return areaManagerVO;
   }

   @Override
   public void saveOrUpdate(AreaManagerVO areaManagerVO) throws InvocationTargetException, IllegalAccessException {
      AreaManager areaManager = new AreaManager();
      BeanUtils.copyProperties(areaManagerVO, areaManager);
      areaManagerService.saveOrUpdate(areaManager);
   }

   @Override
   public void removeById(String id) {
      areaManagerService.removeById(id);
   }

   @Override
   public PageInfo<AreaManagerVO> findPageByVO(AreaManagerVO areaManagerVO, Integer pageNum, Integer pageSize) throws InvocationTargetException, IllegalAccessException {
      PageInfo<AreaManagerVO> pageInfo = areaManagerService.pageByVo(areaManagerVO, pageNum, pageSize);
      return pageInfo;
   }

   @Override
   public void removeByListVo(List<AreaManagerVO> areaManagerVOList) throws IllegalAccessException, InvocationTargetException, InstantiationException {

      if (areaManagerVOList!=null && areaManagerVOList.size()>0){
         for (AreaManagerVO vo : areaManagerVOList) {
            AreaManager one = areaManagerService.getById(vo.getMainId());
            QueryWrapper<AreaManager> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("P_CODE",one.getUserCode());
            areaManagerService.remove(queryWrapper);
            areaManagerService.removeById(vo.getMainId());

         }

      }

   }

   @Override
   public List listByVo(AreaManagerVO areaManagerVO) {
      return areaManagerService.listByVo(areaManagerVO);
   }

   @Override
   public void delByUserCode(String userCode) {
      areaManagerService.delByUserCode(userCode);
   }

   @Override
   public String getBigRegionCodeByCityManagerUserCode(String fellowId) {

      return areaManagerService.getBigRegionCodeByCityManagerUserCode(fellowId);
   }

   @Override
   public PageInfo<AreaManagerVO> pageByUserCode(AreaManagerVO areaManagerVO, Integer pageNum, Integer pageSize) {
      return areaManagerService.pageByUserCode(areaManagerVO,pageNum,pageSize);
   }

   @Override
   public PageInfo<AreaManagerVO> pageCityNotExisit(AreaManagerVO areaManagerVO, Integer pageNum, Integer pageSize) {
      return areaManagerService.pageCityNotExisit(areaManagerVO,pageNum,pageSize);
   }

   @Override
   public List<AreaManagerVO> listByUserCode(AreaManagerVO areaManagerVO) {
      return areaManagerService.listByUserCode(areaManagerVO);
   }

   @Override
   public List<String> getAreaManagerUserCodeByCityManagerUserCode(String fellowId) {
      return areaManagerService.getAreaManagerUserCodeByCityManagerUserCode(fellowId);
   }

   @Override
   public void changeAreaManager(AreaManagerVO areaManagerVO) {
      //原大区经理code
      String pCode = areaManagerVO.getPCode();
      //更换后的大区经理code
      String userCode = areaManagerVO.getUserCode();

      AreaManager areaManager = new AreaManager();
      areaManager.setPCode(userCode);
      QueryWrapper<AreaManager> queryWrapper = new QueryWrapper<>();
      queryWrapper.eq("P_CODE",pCode);
      areaManagerService.update(areaManager,queryWrapper);
   }

}
